<?php

namespace app\api\controller;

use app\admin\model\Crontab;
use app\api\model\project\ProjectStaff;
use app\common\controller\Api;
use app\api\model\project\ProjectOrderComplain as ProjectOrderComplainModel;
use app\api\model\project\ProjectOrderComplainExplain as ProjectOrderComplainExplainModel;
use app\api\model\project\ProjectOrder as ProjectOrderModel;

use think\Db;

//订单投诉操作
class Projectordercomplain extends Api
{

    protected $noNeedRight = ['*'];

    //投诉
    public function complain()
    {
        $order_id = input('order_id',0);
        $reason = input('reason','');
        $explain = input('explain','');
        $images = input('images','');
        $video = input('video','');
        $info = ProjectOrderModel::get($order_id);

        if (!$info)$this->error('订单不存在');
        $complain_info =  ProjectOrderComplainModel::where([ 'user_id'=>$this->auth->id,'order_id'=>$order_id])->find();
        if ($complain_info)$this->error('已投诉');
        $complain_model = new  ProjectOrderComplainModel();

        $data = [
            'user_id'=>$this->auth->id,
            'order_id'=>$order_id,
            'reason'=>$reason,
            'explain'=>$explain,
            'images'=>$images,
            'video'=>$video
        ];

        $res = $complain_model->save($data);
        if ($res){
            $cron = new Crontab();

            $close_time = \app\common\model\Config::get(['name'=>'complain_close_time'])->value;
            $end_time_timestamp = time()+3600*$close_time;

            $cron_data = [
                    'type'=>'url',
                    'title'=>'投诉自动审核,投诉id:'.$complain_model['complain_id'],
                    'content'=>request()->domain().'/api/projectordercomplain/auto?complain_id='.$complain_model['complain_id'],
                    'schedule'=>schedule($end_time_timestamp),
                    'maximums'=>1,
                    'begintime'=>time(),
                    'endtime'=>$end_time_timestamp,
                    'status'=>'normal'
                ];

            $cron->save($cron_data);
            $this->success('投诉成功');
        } else {
            $this->error('投诉失败');
        }
    }

    public function auto()
    {
        $complain_id = input('complain_id',0);

        $complain_info = ProjectOrderComplainModel::get($complain_id);
        $complain_info->status = 2;
        $complain_info->save();
        $this->success('');
    }

    //我收到的投诉
    public function myGetComplain()
    {
        $order_id = ProjectOrderModel::where(['staff_user_id'=>$this->auth->id])->column('order_id');

        $list = ProjectOrderComplainModel::alias('oc')
            ->join('project_order o','oc.order_id = o.order_id')
            ->where('oc.order_id','in',$order_id)
            ->field('o.order_no,oc.*')
            ->order('oc.createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);

        $this->success('',$list);

    }

    //我的投诉
    public function myComplain()
    {
        $list = ProjectOrderComplainModel::alias('oc')
            ->join('project_order o','oc.order_id = o.order_id')
            ->where('oc.user_id',$this->auth->id)
            ->field('o.order_no,oc.*')
            ->order('oc.createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);
        $this->success('',$list);
    }

    //用户撤回投诉
    public function retract()
    {
        $complain_id = input('complain_id',0);
        $complain_info =  ProjectOrderComplainModel::get($complain_id);
        if ($complain_info->status!=0)$this->error('error');
        $complain_info->status = 3;
        $complain_info->save();
        $this->success('撤回成功');
    }

    //我收到的投诉详情
    public function getComplainInfo()
    {
        $complain_id = input('complain_id',0);
        $complain_info =  ProjectOrderComplainModel::get($complain_id);
        if (!$complain_info)$this->error('该投诉不存在');

        $process_list = ProjectOrderComplainExplainModel::alias('oce')
            ->where('oce.complain_id',$complain_id)
            ->order('oce.createtime desc')->select();

        $complain_info['process_list'] = $process_list;

        $complain_info['complain_user_info'] = \app\common\model\User::where(['id'=>$complain_info['user_id']])
            ->field('nickname,avatar')->find();

        $complain_info['order_info'] = ProjectOrderModel::where(['order_id'=>$complain_info['order_id']])
            ->field('order_no,pay_time')->find();
        $this->success('',$complain_info);
    }

    //我的投诉详情
    public function complainInfo()
    {
        $complain_id = input('complain_id',0);
        $complain_info =  ProjectOrderComplainModel::get($complain_id);
        if (!$complain_info)$this->error('该投诉不存在');

        $process_list = ProjectOrderComplainExplainModel::alias('oce')
            ->where('oce.complain_id',$complain_id)
            ->order('oce.createtime desc')->select();

        $complain_info['process_list'] = $process_list;

        $order_info = ProjectOrderModel::where(['order_id'=>$complain_info['order_id']])
            ->field('order_no,pay_time,staff_user_id,staff_id')->find();

        $complain_info['order_info'] = $order_info;

        $complain_info['staff_info'] = ProjectStaff::where(['id'=>$order_info['staff_id']])
            ->field('nickname,realname,image')->find();
        /*$complain_info['user_info'] = \app\common\model\User::where(['id'=>$complain_info['staff_user_id']])
            ->field('nickname,avatar')->find();*/


        $this->success('',$complain_info);
    }

    //服务人员回复
    public function staffReply()
    {
        $complain_id = input('complain_id',0);
        $content = input('content','');
        $images = input('images','');
        $complain_model = new  ProjectOrderComplainExplainModel();
        if (empty($content))$this->error('请输入回复内容');
        $data = [
            'user_id'=>$this->auth->id,
            'type'=>1,
            'complain_id'=>$complain_id,
            'content'=>$content,
            'images'=>$images
        ];
        $res = $complain_model->save($data);
        $this->success('回复成功');
    }

    //下单人回复
    public function reply()
    {
        $complain_id = input('complain_id',0);
        $content = input('content','');
        $images = input('images','');
        $complain_model = new  ProjectOrderComplainExplainModel();
        if (empty($content))$this->error('请输入回复内容');
        $data = [
            'user_id'=>$this->auth->id,
            'type'=>0,
            'complain_id'=>$complain_id,
            'content'=>$content,
            'images'=>$images
        ];
        $res = $complain_model->save($data);
        $this->success('回复成功');
    }


}